package org.example.springblogdemo.service.impl;

import ch.qos.logback.core.util.MD5Util;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotBlank;
import org.example.springblogdemo.constants.Constants;
import org.example.springblogdemo.exception.BlogException;
import org.example.springblogdemo.mapper.UserInfoMapper;
import org.example.springblogdemo.pojo.dataobject.BlogInfo;
import org.example.springblogdemo.pojo.dataobject.UserInfo;
import org.example.springblogdemo.pojo.request.UserLoginRequest;
import org.example.springblogdemo.respone.UserInfoResponse;
import org.example.springblogdemo.respone.UserLoginResponse;
import org.example.springblogdemo.service.BlogService;
import org.example.springblogdemo.service.UserService;
import org.example.springblogdemo.util.BeanTransUtils;
import org.example.springblogdemo.util.JwtUtils;
import org.example.springblogdemo.util.Md5Utils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Resource(name = "userInfoMapper")
    private UserInfoMapper userInfoMapper;
    @Resource(name="blogServiceImpl")
    private BlogService blogService;
    @Override
    public UserLoginResponse check(UserLoginRequest loginRequest) {
        //判断密码是否正确
        UserInfo userInfo=selectUserInfoByName(loginRequest.getUserName());
        if (userInfo==null){
            throw new BlogException("用户不存在");
        }
        if (!Md5Utils.verify(loginRequest.getPassword(),userInfo.getPassword())){
            throw new BlogException("密码错误");
        }
//        if (!userInfo.getPassword().equals(loginRequest.getPassword())){
//            throw new BlogException("密码错误");
//        }
        Map<String ,Object> map= new HashMap<>();
        map.put("id",userInfo.getId());
        map.put("userName",userInfo.getUserName());
        String token= JwtUtils.genJwt(map);
        return new UserLoginResponse(userInfo.getId(),token);
    }

    @Override
    public UserInfoResponse getUserInfo(Integer userId) {
        return BeanTransUtils.transUser(selectUserInfoById(userId));
    }


    @Override
    public UserInfoResponse getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo = blogService.getBlogInfo(blogId);
        if (blogInfo==null){
            throw new BlogException("博客不存在");
        }
        return BeanTransUtils.transUser(selectUserInfoById(blogInfo.getUserId()));
    }

    private UserInfo selectUserInfoByName(@NotBlank(message = "用户名不能为空") String userName) {
        return userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getUserName, userName)
                        .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE)
        );
    }
    private UserInfo selectUserInfoById(Integer userId) {
        return userInfoMapper.selectOne(
                new LambdaQueryWrapper<UserInfo>()
                        .eq(UserInfo::getId, userId)
                        .eq(UserInfo::getDeleteFlag, Constants.NOT_DELETE)
        );
    }

}
